$(document).ready(function () {
    var validate = $("#editForm").validate({
        debug: false,
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function (form) {
            var jsonData = $(form).serializeObject();
            $.ajax({
                url: "EditPayment",
                method: "POST",
                contentType: "application/json;charset=UTF-8",
                async: true,
                dataType: "json",
                data: JSON.stringify(jsonData),
                timeout: timeout,
                success: function (resp) {
                    alert(resp.Message);
                    if (resp.Result) {
                        select("list");
                        query("keep=true", "QueryPayment");
                    }
                }
            }).fail(function (G, F) {
                alert("浏览器超时未响应，请刷新重试");
            });
            return false;
        },

        rules: {
            Name: {
                required: true
            },
            Ratio: {
                required: true,
                range: [0, 1]
            }
        },
        messages: {
            Name: {
                required: "名称不能为空"
            },
            Ratio: {
                required: "分成比例不能为空",
                range: "请输入{0}-{1}之间的数字"
            }
        }

    });

    function A(D) {
        var E = $("table.list tbody");
        E.empty();
        E.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "PaymentPage",
            dataType: "json",
            data: "page=" + D,
            timeout: timeout,
            success: function (F) {
                update(F);
            }
        }).fail(function (G, F) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var B = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: A
    };
    $("div.jpager").paginate(B);
    window.update = function (E) {
        var H = $("table.list tbody");
        var F = E.Items;
        $("table.list input.selall").prop("checked", false);
        H.empty();
        var I = E.Page * 10 - 9;
        if (F.length > 0) {
            for (var D = 0; D < F.length; D++) {
                var G = F[D];
                H.append('<tr><td><input name=ids class="row" type="checkbox" value="' + G.Id + '"/></td><td>' + (I + D) + "</td><td>" + G.Name + "</td><td>" + G.Ratio + '</td><td><button data-id="' + G.Id + '" class="edit btn btn-success"><i class="fa fa-edit fa-lg"></i> 编 辑</button></td></tr>');
            }
        } else {
            H.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        B.start = E.Page;
        B.count = E.Total;
        $("div.jpager").paginate(B);
    };

    function C(D) {
        $("div#edit input").val("");
        if (D) {
            $("div#edit input.Id").val(D.Id);
            $("div#edit input.Name").val(D.Name);
            $("div#edit input.Ratio").val(D.Ratio);
        }
    }
    $("div.query button.query").click(function (D) {
        query($("div.query form").serialize(), "QueryPayment");
        return false;
    });
    query($("div.query form").serialize(), "QueryPayment");
    $("ul.tool button.add").click(function (D) {
        C(null);
        select("edit");
        return false;
    });

    $("button.cancel").click(function (D) {
        validate.resetForm();
        select("list");
        return false;
    });
    $("ul.tool button.delete").click(function (D) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var E = $("table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeletePayments",
                dataType: "json",
                data: E,
                timeout: timeout,
                success: function (F) {
                    alert(F.Message);
                    if (F.Result) {
                        query("keep=true", "QueryPayment");
                    }
                }
            }).fail(function (G, F) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
    $("table.list tbody").click(function (D) {
        var E = $(D.target);
        if (E.is("button.delete")) {
            if (confirm("您确定要删除该记录吗？")) {
                var F = E.data("id");
                $.ajax({
                    url: "DeletePayment",
                    dataType: "json",
                    data: "Id=" + F,
                    timeout: timeout,
                    success: function (G) {
                        alert(G.Message);
                        if (G.Result) {
                            query("keep=true", "QueryPayment");
                        }
                    }
                }).fail(function (H, G) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        } else {
            if (E.is("button.edit")) {
                var F = E.data("id");
                $.ajax({
                    url: "GetPayment",
                    dataType: "json",
                    data: "Id=" + F,
                    timeout: timeout,
                    success: function (G) {
                        C(G);
                        select("edit");
                    }
                }).fail(function (H, G) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        }
    });
});